package com.stlm2.dems.user.service;

import com.stlm2.dems.entity.User;
import com.stlm2.dems.service.UserService;
import com.stlm2.util.Convert;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;

import com.stlm2.dems.user.entity.Lower;
import com.stlm2.dems.user.dao.LowerDao;
import com.stlm2.core.base.BaseDao;
import com.stlm2.core.base.impl.BaseServiceImpl;
import org.springframework.ui.ModelMap;

import java.util.ArrayList;
import java.util.List;

@Service("lowerService")
@Transactional(rollbackForClassName={"java.lang.Exception"}, propagation = Propagation.REQUIRED)
public class LowerService extends BaseServiceImpl<Lower> {

	@Resource
	private LowerDao lowerDao;
	@Resource
	private UserService userService;

	@Override
	protected BaseDao <Lower> getDao() {
		return lowerDao;
	}

	public List<Lower> getOneLower(Integer userId) {
		return lowerDao.getOneLower(userId);
	}

	public List<Lower> getTwoLower(Integer userId) {
		return lowerDao.getTwoLower(userId);
	}

	public List<Lower> getThreeLower(Integer userId) {
		return lowerDao.getThreeLower(userId);
	}

	public List<ModelMap> convent(List<Lower> lowers) {
		List<ModelMap> list=new ArrayList<ModelMap>();
		for(Lower lower:lowers){
			list.add(buiderModel(lower));
		}
		return list;
	}

	//一级，账号+*、昵称、注册时间。
	private ModelMap buiderModel(Lower lower) {
		User user=userService.findEntityById(lower.getLowerUserId());
		ModelMap map=new ModelMap();
		map.put("name",user.getName().replaceAll("(\\d{3})\\d{4}(\\d{4})","$1****$2"));
		map.put("realNmae",user.getRealName()==null?"":user.getRealName());
		map.put("createTime", Convert.toFullYMDHMS(user.getCreateTime()));
		return map;
	}
}